﻿using System;
using System.Net;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using System.Reflection;
using AbstraX.XPathBuilder;
using System.Linq;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Server;
using System.Diagnostics;
using System.Collections.Generic;
using AssemblyProvider.Web.Entities;
using ControlProviders.ProviderInterfaces;
using AbstraX;
using AbstraX.Templates;
using MvvmTreeView;
using AbstraX.Contracts;

namespace SLControlProvider.Web.Entities
{
    [DataContract, DebuggerDisplay("{ DebugInfo }"), ClientCodeGeneration(typeof(AbstraXClientInterfaceGenerator))]
    public class SLMethodOperation : SLOperation, IControlOperation
    {
        private MethodOperation method;
        protected IBase parent;
        protected IProviderEntityService providerEntityService;

        public SLMethodOperation()
        {
        }
       
        public IAbstraXExtension LoadExtension()
        {
            return null;
        }

        public SLMethodOperation(MethodOperation method, IBase parent)
        {
            this.method = method;
            this.parent = parent;

            providerEntityService = ((IBase)parent).ProviderEntityService;
        }

        [Exclude]
        IProviderEntityService IBase.ProviderEntityService
        {
            get
            {
                return providerEntityService;
            }
        }

        [DataMember]
        public override float ChildOrdinal
        {
            get
            {
                return method.ChildOrdinal;
            }
        }

        [DataMember]
        public override string DebugInfo
        {
            get
            {
                return method.DebugInfo;
            }
        }

        [DataMember]
        public override OperationDirection Direction
        {
            get 
            {
                return method.Direction;
            }
        }

        public override IEnumerable<IElement> ChildElements
        {
            get 
            {
                return method.ChildElements;
            }
        }

        [DataMember]
        public override string ParentID
        {
            get
            {
                return method.ParentID;
            }
        }

        [DataMember]
        public override string Name
        {
            get 
            {
                return method.Name;
            }
        }

        [DataMember]
        public override string DesignComments
        {
            get
            {
                return method.DesignComments;
            }
        }

        [DataMember]
        public override string DocumentationSummary
        {
            get
            {
                return method.DocumentationSummary;
            }
        }

        [DataMember]
        public override string Documentation
        {
            get
            {
                return method.Documentation;
            }
        }

        [DataMember]
        public override bool HasDocumentation
        {
            get
            {
                return method.HasDocumentation;
            }
        }

        [DataMember]
        public override string ImageURL
        {
            get
            {
                return method.ImageURL;
            }
        }

        [DataMember]
        public override IBase Parent
        {
            get
            {
                return method.Parent;
            }
        }


        [DataMember]
        public override string FolderKeyPair
        {
            get
            {
                return method.FolderKeyPair;
            }

            set
            {
                method.FolderKeyPair = value;
            }
        }

        public override IRoot Root
        {
            get
            {
                IBase baseObject = this;

                while (baseObject != null)
                {
                    baseObject = baseObject.Parent;

                    if (baseObject is IRoot)
                    {
                        return (IRoot)baseObject;
                    }
                }

                return null;
            }
        }

        public override IQueryable ExecuteWhere(string property, object value)
        {
            method.ExecuteWhere(property, value);

            return method.ChildElements.AsQueryable();
        }

        public override IQueryable ExecuteWhere(System.Linq.Expressions.Expression expression)
        {
            method.ExecuteWhere(expression);

            return method.ChildElements.AsQueryable();
        }

        public override IQueryable ExecuteWhere(XPathAxisElement element)
        {
            method.ExecuteWhere(element);

            return method.ChildElements.AsQueryable();
        }

        public override void ClearPredicates()
        {
            method.ClearPredicates();
        }

        [DataMember]
        public override DefinitionKind Kind
        {
            get
            {
                return method.Kind;
            }
        }

        [DataMember]
        public override bool HasChildren
        {
            get
            {
                return method.HasChildren;
            }
        }

        [DataMember]
        public override Modifiers Modifiers
        {
            get
            {
                return method.Modifiers;
            }
        }

        [DataMember, Include, Association("Parent_Facet", "ID", "ParentID")]
        public Facet[] Facets
        {
            get { throw new NotImplementedException(); }
        }
    }
}
